﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGGCore;

namespace IGGConsole
{
    public class ModeTeamFormatter : IModeFormatter
    {
        #region IModeFormatter Members

        private void FormatPos(Player[] pos, string posLabel)
        {
            if (pos == null || pos.Length == 0)
            {
                return;
            }
            if (pos.Length == 1)
            {
                Console.WriteLine(" " + posLabel + ": " + ToolBox.GetFormattedStringPadRight(pos[0].FullName, 20));
            }
            else if (pos.Length == 2)
            {
                Console.WriteLine(" " + posLabel + ": " + ToolBox.GetFormattedStringPadRight(pos[0].FullName, 20) + pos[1].FullName);
            }
            else if (pos.Length > 2)
            {
                Console.WriteLine(" " + posLabel + ": " + ToolBox.GetFormattedStringPadRight(pos[0].FullName, 20) + ToolBox.GetFormattedStringPadRight(pos[1].FullName, 20) + pos[2].FullName);
            }
        }

        /// <summary>
        /// Format the history tidbit
        /// 
        /// Boston Red Sox 
        /// 34 - 23. Third in the East (8 GB). Second in Wild-Card (1 GB).
        /// 
        /// The team is on a 5 games losing streak :-(
        /// They lost their last game to the Royals 8 to 3. Oswalt was the losing pitcher.
        /// 
        /// 
        /// </summary>
        public void FormatMode(Scoreboard board)
        {
            Random rnd = new Random();
            Console.WriteLine();
            Console.WriteLine(" " + board.ActiveTeam.City + " " + board.ActiveTeam.Name);
            if (board.ActiveTeam.Facts != null && board.ActiveTeam.Facts.Count > 0)
            {
                StringFormatter.FormatLongText(board.ActiveTeam.Facts[rnd.Next(board.ActiveTeam.Facts.Count)]);
            }
            int i = 0;
            int count = 0;
            bool finished = false; 
            while(!finished)
            {
                if (board.ActiveTeam.News[i].HeadlineRatio > -100)
                {
                    Console.WriteLine();
                    Console.WriteLine(" " + board.ActiveTeam.News[i].Title.ToUpper());
                    StringFormatter.FormatLongText(board.ActiveTeam.News[i].Content);
                    count++;
                }
                if (count == 3)
                {
                    finished = true;
                }
                i++;
            }

            // Depth Chart
            Console.WriteLine();
            FormatPos(board.ActiveTeam.DepthChart.FirstBases, "1B");
            FormatPos(board.ActiveTeam.DepthChart.SecondBases, "2B");
            FormatPos(board.ActiveTeam.DepthChart.ThirdBases, "3B");
            FormatPos(board.ActiveTeam.DepthChart.Shortstops, "SS");
            FormatPos(board.ActiveTeam.DepthChart.LeftFields, "LF");
            FormatPos(board.ActiveTeam.DepthChart.CenterFields, "CF");
            FormatPos(board.ActiveTeam.DepthChart.RightFields, "RF");
            FormatPos(board.ActiveTeam.DepthChart.Catchers, " C");
            FormatPos(board.ActiveTeam.DepthChart.DesignatedHitters, "DH");

            Console.WriteLine("");
            for (int po = 0; po < 5; po++)
            {
                Console.WriteLine(" SP: " + board.ActiveTeam.DepthChart.Pitchers[po].FullName);
            }
            Console.WriteLine("");
            Console.WriteLine(" CL: " + board.ActiveTeam.DepthChart.Closer.FullName);
            for (int pi = 1; pi < 10; pi++)
            {
                if (board.ActiveTeam.DepthChart.Relievers.Length > pi && board.ActiveTeam.DepthChart.Relievers[pi] != null)
                {
                    Console.WriteLine(" MR: " + board.ActiveTeam.DepthChart.Relievers[pi].FullName);
                }
            }

            // DL
            if (board.ActiveTeam.DepthChart.DLs != null && board.ActiveTeam.DepthChart.DLs.Length > 0)
            {
                Console.WriteLine("");
                for (int po = 0; po < board.ActiveTeam.DepthChart.DLs.Length; po++)
                {
                    Console.WriteLine(" DL: " + board.ActiveTeam.DepthChart.DLs[po].FullName);
                }
            }

            // Beat Writers Tweets
            if (board.ActiveTeam.BeatTweets != null && board.ActiveTeam.BeatTweets.Length > 0)
            {
                for (i = 0; i < board.ActiveTeam.BeatTweets.Length; i++)
                {
                    Console.WriteLine();
                    Console.WriteLine(" BEAT - " + board.ActiveTeam.BeatTweets[i].Author.ToUpper());
                    StringFormatter.FormatLongText(board.ActiveTeam.BeatTweets[i].Text);
                }
            }



            Console.CursorTop = 0;
            Console.CursorLeft = 0;
            // Get cursor at the top!
        }

        #endregion
    }
}
